import React from "react";
import { Activity, Heart, Scale, Moon } from "lucide-react";
import { HealthCard } from "./HealthCard";
import type { HealthRecord } from "@/services/health.service";

interface HealthStatsProps {
  latestRecords: {
    weight?: HealthRecord;
    bloodPressure?: HealthRecord;
    heartRate?: HealthRecord;
    sleepQuality?: HealthRecord;
  };
  records: HealthRecord[];
}

export function HealthStats({ latestRecords, records }: HealthStatsProps) {
  const calculateTrend = (type: HealthRecord['recordType']): { type: 'up' | 'down'; value: string; description: string; } | undefined => {
    const typeRecords = records.filter(r => r.recordType === type);
    if (typeRecords.length < 2) return undefined;

    const latest = typeRecords[0];
    const previous = typeRecords[1];
    
    switch (type) {
      case 'weight':
        if (!latest.details.weight || !previous.details.weight) return undefined;
        const weightDiff = latest.details.weight.value - previous.details.weight.value;
        return {
          type: weightDiff > 0 ? 'up' : 'down',
          value: `${Math.abs(weightDiff).toFixed(1)}${latest.details.weight.unit}`,
          description: '较上次'
        };

      case 'bloodPressure':
        if (!latest.details.bloodPressure || !previous.details.bloodPressure) return undefined;
        const systolicDiff = latest.details.bloodPressure.systolic - previous.details.bloodPressure.systolic;
        return {
          type: systolicDiff > 0 ? 'up' : 'down',
          value: `${Math.abs(systolicDiff)}mmHg`,
          description: '较上次'
        };

      case 'heartRate':
        if (!latest.details.heartRate || !previous.details.heartRate) return undefined;
        const heartRateDiff = latest.details.heartRate.current - previous.details.heartRate.current;
        return {
          type: heartRateDiff > 0 ? 'up' : 'down',
          value: `${Math.abs(heartRateDiff)}bpm`,
          description: '较上次'
        };

      case 'sleepQuality':
        if (!latest.details.sleepQuality || !previous.details.sleepQuality) return undefined;
        const sleepDiff = latest.details.sleepQuality.totalSleepDuration - previous.details.sleepQuality.totalSleepDuration;
        return {
          type: sleepDiff > 0 ? 'up' : 'down',
          value: `${Math.abs(sleepDiff).toFixed(1)}小时`,
          description: '较上次'
        };

      default:
        return undefined;
    }
  };

  return (
    <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-12">
      <HealthCard
        record={latestRecords.weight}
        type="weight"
        title="体重"
        subtitle="保持健康体重"
        value={
          latestRecords.weight?.details?.weight
            ? `${latestRecords.weight.details.weight.value} ${latestRecords.weight.details.weight.unit}`
            : null
        }
        trend={calculateTrend('weight')}
      />
      <HealthCard
        record={latestRecords.bloodPressure}
        type="bloodPressure"
        title="血压"
        subtitle="关注心血管健康"
        value={
          latestRecords.bloodPressure?.details?.bloodPressure
            ? `${latestRecords.bloodPressure.details.bloodPressure.systolic}/${latestRecords.bloodPressure.details.bloodPressure.diastolic} mmHg`
            : null
        }
        trend={calculateTrend('bloodPressure')}
      />
      <HealthCard
        record={latestRecords.heartRate}
        type="heartRate"
        title="心率"
        subtitle="监测心脏状况"
        value={
          latestRecords.heartRate?.details?.heartRate
            ? `${latestRecords.heartRate.details.heartRate.current} bpm`
            : null
        }
        trend={calculateTrend('heartRate')}
      />
      <HealthCard
        record={latestRecords.sleepQuality}
        type="sleepQuality"
        title="睡眠"
        subtitle="享受优质睡眠"
        value={
          latestRecords.sleepQuality?.details?.sleepQuality
            ? `${latestRecords.sleepQuality.details.sleepQuality.totalSleepDuration} 小时`
            : null
        }
        trend={calculateTrend('sleepQuality')}
      />
    </div>
  );
} 